﻿//功能，页面窗口，将网页中已存在的元素转为浮动窗口
//不能生成窗口，html进行控制
//开始日期2009-12-25
//舒彩光
//基础控件为 Action.js
//示例:var d=new Page.Dialog($$("domid"));d.SetCloseButton($$("buttonid"));
Function.registerNameSpace("Page");

//初始化，参数为obj,用obj不用ID原因为更好的通用脚本生成的控件,mode为窗口是否为独占窗口，为布尔值
Page.Dialog=function(obj,mode){
    Function.registerClass(this);//必须有
    this._action=new Page.Action(obj);
    this._mode=mode;
    this._obj=obj;
    this._movestart=false;
    
    this._closeButton_events={
        click:EventOperate.createDelegate(this,this._closeButton_Click)
    };
    this._moveBar_events={
        mousedown:EventOperate.createDelegate(this,this._moveBar_Active)
    };
    this._document_events={
        mousemove:EventOperate.createDelegate(this,this._moveBar_Move)
        ,mouseup:EventOperate.createDelegate(this,this._moveBar_End)
    };
    this._window_events={resize:EventOperate.createDelegate(this,this._dialog_Out)};
    EventOperate.addEvents(window,this._window_events);
    obj.style.display="none";
}

//设定关闭按钮
Page.Dialog.prototype.SetCloseButton=function(obj){
    if(typeof(obj)=="string") obj=$$(obj);
    this._closeButton=obj;
    EventOperate.addEvents(obj,this._closeButton_events);
}

//设定可移动区
Page.Dialog.prototype.SetMoveBar=function(obj){
    if(typeof(obj)=="string") obj=$$(obj);
    this._moveBar=obj;
    EventOperate.FixedPart(obj);
    EventOperate.addEvents(obj,this._moveBar_events);
    EventOperate.addEvents(document,this._document_events);
}

//打开窗口
Page.Dialog.prototype.Open=function(position){
    if(this._mode) document.toBack(true);
    this._obj.style.display="block";
    if(position) this._action.ToFixedPosition(position);
    else this._action.ToFixedCenter();
}

//关闭窗口
Page.Dialog.prototype.Close=function(){
    if(this._mode) document.toBack(false);
    this._obj.style.display="none";
}

//点击半闭按钮
Page.Dialog.prototype._closeButton_Click=function(sender,e){
    this.Close();
}

//移动准备
Page.Dialog.prototype._moveBar_Active=function(sender,e){
    if(this._movestart) return;
    if(this._moveBar){
        this._movestart=true;
        this._mouseX=e.clientX;
        this._mouseY=e.clientY;
        this._moveBarX=this._moveBar.clientX;
        this._moveBarY=this._moveBar.clientY;
    }
}

//正在移动
Page.Dialog.prototype._moveBar_Move=function(sender,e){
    if(this._movestart){
        var _mouseX=e.clientX;
        var _mouseY=e.clientY;
        var position={left:this._obj.offsetLeft+_mouseX-this._mouseX-document.getScrollLeft(),top:this._obj.offsetTop+_mouseY-this._mouseY-document.getScrollTop()};
        this._action.ToFixedPosition(position);
        this._mouseX=_mouseX;
        this._mouseY=_mouseY;
    }
}

//移动结束
Page.Dialog.prototype._moveBar_End=function(sender,e){
    this._movestart=false;
}

//出界
Page.Dialog.prototype._dialog_Out=function(sender,e){
    var position={};
    if(this._obj.offsetLeft+this._obj.offsetWidth>document.getScrollLeft()+document.getClientWidth()){
        position.right=0;
        position.top=null;
        position.bottom=0;
        this._action.ToFixedPosition(position);
    }
    if(this._obj.offsetTop+this._obj.offsetHeight>document.getScrollTop()+document.getClientHeight()){
        position.right=0;
        position.top=null;
        position.bottom=0;
        this._action.ToFixedPosition(position);
    }
}

//必须有
Page.Dialog.prototype.dispose=function(){
    this._action=null;
    this._obj=null;
    this._closeButton=null;
    this._closeButton_events=null;
}